package june.sdsearchtest;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class SDsearchTestActivity extends Activity {
    /** Called when the activity is first created. */
	
	ListView search_list;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        search_list = (ListView)findViewById(R.id.search_list);
        
        SDsearchAsyncTesk sdsearchAsyncTesk = new SDsearchAsyncTesk(this, ".mp3", resultHandler);
        sdsearchAsyncTesk.execute();
    }
    
    Handler resultHandler = new Handler(){
    	@Override
    	public void handleMessage(Message msg) {
    		//성공일때 
    		if(msg.arg1 == 100){
    			ArrayList<String[]> result = (ArrayList<String[]>)msg.obj;
    			String[] searchfile = result.get(0);
    			//결과확인 제대로 됬다면 담겨있는 첫번째 결과값이 찍힌다
    			Log.e("june", "searchfile : " + searchfile[0] + ", " + searchfile[1]);
    			//리스트를 구성하든 뭘 하든 이제 이 아랫줄로. 
    			SearchListAdapter searchListAdapter = new SearchListAdapter(SDsearchTestActivity.this, result);
    			search_list.setAdapter(searchListAdapter);
    		}
    		//실패일때 
    		else if(msg.arg1 == 101){
    			Log.e("june", "검색이 캔슬됨");
    		}
    	}
    };

    /**
     * songlist 어답터.
     * @author HackJuneLee
     *
     */
    class SearchListAdapter extends BaseAdapter{

    	Context context;
    	ArrayList<String[]> search_list;
    	LayoutInflater inflater;
    	
    	public SearchListAdapter(Context context, ArrayList<String[]> search_list){
    		this.context = context;
    		this.search_list = search_list;
    		inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	}
    	
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return search_list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return search_list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View view, ViewGroup vg) {
			
			if(view == null){
				view = inflater.inflate(R.layout.search_item, vg, false);
			}
			
			TextView search_name = (TextView)view.findViewById(R.id.search_name);
			search_name.setText(search_list.get(position)[0]);
			
			TextView search_path = (TextView)view.findViewById(R.id.search_path);
			search_path.setText(search_list.get(position)[1]);
			
			return view;
		}
    	
    }
    
    
    
}